<!DOCTYPE html>
<html lang="zh-CN">





<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="https://img2.jimu98.cn/blog/20200729222712.png">
  <link rel="icon" type="image/png" href="https://img2.jimu98.cn/blog/20200729222712.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="你会的越多,你不会的越多,我是积木,互联网的一只小菜鸡">
  <meta name="author" content="积木">
  <meta name="keywords" content="积木,jimu98,java,后端,jimu">
  <title>【项目笔记】ES6复习 - 积木 | 互联网的一只小菜鸡</title>

  <link  rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.staticfile.org/github-markdown-css/4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    <link  rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/10.0.0/styles/github-gist.min.css" />
  

  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_yg9cfy8wd6.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_pjno9b9zyxs.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script  src="/js/utils.js" ></script>
  <script data-ad-client="ca-pub-2806771379080352" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<meta name="generator" content="Hexo 4.2.1"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>jimu98</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/list/">
                <i class="iconfont icon-bookmark-fill"></i>
                导航
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/links/">
                <i class="iconfont icon-link-fill"></i>
                友链
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="view intro-2" id="background" parallax=true
         style="background: url('https://img2.jimu98.cn/blog/20200729222405.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="container text-center white-text fadeInUp">
            <span class="h2" id="subtitle">
              
            </span>

            
              
  <div class="mt-3 post-meta">
    <i class="iconfont icon-date-fill" aria-hidden="true"></i>
    <time datetime="2020-07-22 21:06">
      2020年7月22日 晚上
    </time>
  </div>


<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.5k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      22
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid">
  <div class="row">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-md">
      <div class="container nopadding-md" id="board-ctn">
        <div class="py-5" id="board">
          <div class="post-content mx-auto" id="post">
            
            <article class="markdown-body">
              <h1 id="【项目笔记】ES6复习"><a href="#【项目笔记】ES6复习" class="headerlink" title="【项目笔记】ES6复习"></a>【项目笔记】ES6复习</h1><h2 id="ES6介绍"><a href="#ES6介绍" class="headerlink" title="ES6介绍"></a>ES6介绍</h2><p>ECMAScript6.0是JavaScript语言的下一代标准。。。。。。等等介绍。。</p>
<h2 id="let语法"><a href="#let语法" class="headerlink" title="let语法"></a>let语法</h2><pre><code class="hljs js"><span class="hljs-comment">//var声明的变量往往会越域</span>
<span class="hljs-comment">//let声明的变量有严格的局部作用域</span>
&#123;
    <span class="hljs-keyword">var</span> a = <span class="hljs-number">1</span>
    <span class="hljs-keyword">let</span> b = <span class="hljs-number">2</span>
    &#125;
<span class="hljs-built_in">console</span>.log(a) <span class="hljs-comment">//1</span>
<span class="hljs-built_in">console</span>.log(b)  <span class="hljs-comment">//b is not defined</span>

<span class="hljs-comment">//var可以声明多次</span>
<span class="hljs-comment">//let只能声明一次</span>

<span class="hljs-keyword">var</span> m = <span class="hljs-number">1</span>
<span class="hljs-keyword">var</span> m = <span class="hljs-number">2</span>
<span class="hljs-keyword">let</span> n = <span class="hljs-number">3</span>
<span class="hljs-keyword">let</span> n = <span class="hljs-number">4</span>  <span class="hljs-comment">//报错</span>

<span class="hljs-comment">//var 会变量提升</span>
<span class="hljs-comment">//let 不存在变量提升</span>
<span class="hljs-built_in">console</span>.log(x)  <span class="hljs-comment">//undefined</span>
<span class="hljs-keyword">var</span> x = <span class="hljs-number">10</span>
<span class="hljs-built_in">console</span>.log(y)  <span class="hljs-comment">//y is not defined</span>
<span class="hljs-keyword">var</span> y = <span class="hljs-number">20</span>

<span class="hljs-comment">//因此，以后我们经常使用let生成变量</span>
<span class="hljs-comment">//同时我们也经常使用const声明常量  声明之后就不可以再改变</span></code></pre>

<h2 id="解构表达式"><a href="#解构表达式" class="headerlink" title="解构表达式"></a>解构表达式</h2><pre><code class="hljs js"><span class="hljs-keyword">let</span> arr = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]

<span class="hljs-comment">//原来</span>
<span class="hljs-keyword">let</span> a = arr[<span class="hljs-number">0</span>]
<span class="hljs-keyword">let</span> b = arr[<span class="hljs-number">1</span>]
<span class="hljs-keyword">let</span> c = arr[<span class="hljs-number">2</span>]

<span class="hljs-built_in">console</span>.log(a, b, c)

<span class="hljs-comment">//现在</span>
<span class="hljs-keyword">let</span> [x, y, z] = arr

<span class="hljs-built_in">console</span>.log(x, y, z)

<span class="hljs-keyword">const</span> person = &#123;
    name: <span class="hljs-string">"jimu98"</span>,
    age: <span class="hljs-number">21</span>,
    language: [<span class="hljs-string">'java'</span>, <span class="hljs-string">'js'</span>, <span class="hljs-string">'css'</span>]
&#125;

<span class="hljs-comment">//原来</span>
<span class="hljs-comment">// const name = person.name</span>
<span class="hljs-comment">// const age = person.age</span>
<span class="hljs-comment">// const language = person.language</span>

<span class="hljs-comment">//现在</span>
<span class="hljs-keyword">const</span> &#123; <span class="hljs-attr">name</span>:abc, age, language &#125; = person
<span class="hljs-built_in">console</span>.log(name, age, language)</code></pre>

<h2 id="字符串模板"><a href="#字符串模板" class="headerlink" title="字符串模板"></a>字符串模板</h2><pre><code class="hljs js"><span class="hljs-keyword">let</span> str=<span class="hljs-string">`&lt;div&gt;</span>
<span class="hljs-string">            &lt;span&gt;hello jimu98&lt;/span&gt;</span>
<span class="hljs-string">        &lt;/div&gt;`</span>
<span class="hljs-comment">//以前这些内容需要字符串拼接，现在只需要一个反引号</span></code></pre>

<h3 id="字符串插入变量和表达式"><a href="#字符串插入变量和表达式" class="headerlink" title="字符串插入变量和表达式"></a>字符串插入变量和表达式</h3><pre><code class="hljs js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fun</span>(<span class="hljs-params"></span>)</span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-string">"这是一个函数"</span>
&#125;
<span class="hljs-keyword">let</span> info =<span class="hljs-string">`我是<span class="hljs-subst">$&#123;name&#125;</span>,今年<span class="hljs-subst">$&#123;age+<span class="hljs-number">10</span>&#125;</span>了，我想说<span class="hljs-subst">$&#123;fun()&#125;</span>`</span>
<span class="hljs-built_in">console</span>.log(info)</code></pre>

<h2 id="函数优化"><a href="#函数优化" class="headerlink" title="函数优化"></a>函数优化</h2><h3 id="函数参数默认值"><a href="#函数参数默认值" class="headerlink" title="函数参数默认值"></a>函数参数默认值</h3><pre><code class="hljs js"><span class="hljs-comment">//在ES6以前，我们无法给一个函数参数设置默认值，只能采用变通写法。</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a,b</span>)</span>&#123;
    b=b || <span class="hljs-number">1</span>
    <span class="hljs-keyword">return</span> a+b
&#125;
<span class="hljs-built_in">console</span>.log(add(<span class="hljs-number">10</span>))
<span class="hljs-comment">//现在可以写默认值</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add2</span>(<span class="hljs-params">a,b=<span class="hljs-number">1</span></span>)</span>&#123;
    <span class="hljs-keyword">return</span> a+b
&#125;
<span class="hljs-built_in">console</span>.log(add(<span class="hljs-number">20</span>))</code></pre>

<h3 id="不定参数"><a href="#不定参数" class="headerlink" title="不定参数"></a>不定参数</h3><pre><code class="hljs js"><span class="hljs-comment">//不定参数</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fun</span>(<span class="hljs-params">...values</span>)</span>&#123;
    <span class="hljs-built_in">console</span>.log(values.length)
&#125;
fun(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)
fun(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>)</code></pre>

<h3 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h3><pre><code class="hljs js"><span class="hljs-comment">//以前声明一个方法</span>
<span class="hljs-keyword">var</span> print1 = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">obj</span>) </span>&#123;
    <span class="hljs-built_in">console</span>.log(obj)
&#125;
<span class="hljs-comment">//现在</span>
<span class="hljs-keyword">var</span> print = <span class="hljs-function"><span class="hljs-params">obj</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(obj)
print(<span class="hljs-string">"Hello"</span>)
<span class="hljs-comment">//多个参数</span>
<span class="hljs-keyword">var</span> sum = <span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a + b
print(sum(<span class="hljs-number">1</span>,<span class="hljs-number">3</span>))

<span class="hljs-comment">//箭头函数+解构</span>
<span class="hljs-keyword">var</span> hello2 = <span class="hljs-function">(<span class="hljs-params">&#123; name &#125;</span>) =&gt;</span> <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"hello,"</span> + name)
hello2(person)</code></pre>

<h2 id="对象优化"><a href="#对象优化" class="headerlink" title="对象优化"></a>对象优化</h2><h3 id="新增的API"><a href="#新增的API" class="headerlink" title="新增的API"></a>新增的API</h3><pre><code class="hljs js"><span class="hljs-keyword">const</span> person = &#123;
    name: <span class="hljs-string">"jimu98"</span>,
    age: <span class="hljs-number">21</span>,
    language: [<span class="hljs-string">'java'</span>, <span class="hljs-string">'js'</span>, <span class="hljs-string">'css'</span>]
&#125;
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.keys(person))<span class="hljs-comment">//["name", "age", "language"]</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.values(person))<span class="hljs-comment">//["jimu98", 21, Array(3)]</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.entries(person)) <span class="hljs-comment">//[Array(2), Array(2), Array(2)]</span>

<span class="hljs-keyword">const</span> target = &#123; <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> &#125;
<span class="hljs-keyword">const</span> source1 = &#123; <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> &#125;
<span class="hljs-keyword">const</span> source2 = &#123; <span class="hljs-attr">c</span>: <span class="hljs-number">3</span> &#125;

<span class="hljs-built_in">Object</span>.assign(target, source1, source2)
<span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">Object</span>.keys(target))<span class="hljs-comment">//["a", "b", "c"]</span></code></pre>

<h3 id="声明对象的简写"><a href="#声明对象的简写" class="headerlink" title="声明对象的简写"></a>声明对象的简写</h3><pre><code class="hljs js"><span class="hljs-comment">//声明对象简写</span>
<span class="hljs-keyword">const</span> age = <span class="hljs-number">23</span>
<span class="hljs-keyword">const</span> name = <span class="hljs-string">"jimu98"</span>
<span class="hljs-comment">//原来</span>
<span class="hljs-keyword">const</span> person1 = &#123; <span class="hljs-attr">age</span>: age, <span class="hljs-attr">name</span>: name &#125;
<span class="hljs-comment">//现在</span>
<span class="hljs-keyword">const</span> person2=&#123;age,name&#125;
<span class="hljs-built_in">console</span>.log(person2)</code></pre>

<h3 id="函数的简写"><a href="#函数的简写" class="headerlink" title="函数的简写"></a>函数的简写</h3><pre><code class="hljs js"><span class="hljs-keyword">let</span> person = &#123;
    name: <span class="hljs-string">"jack"</span>,
    <span class="hljs-comment">//原来</span>
    eat: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">food</span>) </span>&#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.name + <span class="hljs-string">"在吃"</span> + food)
    &#125;,
    eat2: <span class="hljs-function"><span class="hljs-params">food</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(person.name + <span class="hljs-string">"在吃"</span> + food),<span class="hljs-comment">//这里不能用this</span>
    eat3(food)&#123;
        <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.name + <span class="hljs-string">"在吃"</span> + food)
    &#125;
&#125;
person.eat(<span class="hljs-string">"香蕉"</span>)
person.eat2(<span class="hljs-string">"苹果"</span>)
person.eat3(<span class="hljs-string">"梨"</span>)</code></pre>

<h3 id="对象拓展运算符"><a href="#对象拓展运算符" class="headerlink" title="对象拓展运算符"></a>对象拓展运算符</h3><pre><code class="hljs js"><span class="hljs-comment">// 1、拷贝对象（深拷贝）</span>
<span class="hljs-keyword">let</span> p1 = &#123; <span class="hljs-attr">name</span>: <span class="hljs-string">"Amy"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">15</span> &#125;
<span class="hljs-keyword">let</span> someone = &#123; ...p1 &#125;
<span class="hljs-built_in">console</span>.log(someone)  <span class="hljs-comment">//&#123;name: "Amy", age: 15&#125;</span>

<span class="hljs-comment">// 2、合并对象</span>
<span class="hljs-keyword">let</span> age1 = &#123; <span class="hljs-attr">age</span>: <span class="hljs-number">15</span> &#125;
<span class="hljs-keyword">let</span> name1 = &#123; <span class="hljs-attr">name</span>: <span class="hljs-string">"Amy"</span> &#125;
<span class="hljs-keyword">let</span> p2 = &#123;<span class="hljs-attr">name</span>:<span class="hljs-string">"zhangsan"</span>&#125;
p2 = &#123; ...age1, ...name1 &#125; <span class="hljs-comment">//如果两个对象的字段名重复，后面对象字段值会覆盖前面对象的字段值</span>
<span class="hljs-built_in">console</span>.log(p2)</code></pre>

<p>map和reduce方法</p>
<p>//map()接受一个函数，将原数组中的所有元素用这个函数处理后放入新数组返回。</p>
<pre><code class="hljs js"><span class="hljs-comment">//数组中新增了map和reduce方法。</span>
<span class="hljs-comment">//map()：接收一个函数，将原数组中的所有元素用这个函数处理后放入新数组返回。</span>
<span class="hljs-keyword">let</span> arr = [<span class="hljs-string">'1'</span>, <span class="hljs-string">'20'</span>, <span class="hljs-string">'-5'</span>, <span class="hljs-string">'3'</span>];
<span class="hljs-comment">//原来</span>
arr = arr.map(<span class="hljs-function">(<span class="hljs-params">item</span>) =&gt;</span> &#123;
    <span class="hljs-keyword">return</span> item * <span class="hljs-number">2</span>
&#125;);
<span class="hljs-built_in">console</span>.log(arr)
<span class="hljs-comment">//现在</span>
arr = arr.map(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item * <span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(arr)

<span class="hljs-comment">//传入回调函数</span>
<span class="hljs-keyword">let</span> res = arr.reduce(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"上一次处理后"</span> + a)
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"当前正在处理的"</span> + b)
    <span class="hljs-keyword">return</span> a + b;
&#125;)
<span class="hljs-built_in">console</span>.log(res)</code></pre>

<h2 id="promist"><a href="#promist" class="headerlink" title="promist"></a>promist</h2><blockquote>
<p>这个是用来处理异步</p>
<p>在之前我们写ajax中，需要一层层嵌套，通过这个就可以使用then关键字</p>
<p>最后还可以抽取出来公共代码，使得代码更加优雅</p>
</blockquote>
<pre><code class="hljs js"><span class="hljs-comment">//1、查出当前用户信息</span>
<span class="hljs-comment">//2、按照当前用户的id查出他的课程</span>
<span class="hljs-comment">//3、按照当前课程id查出分数</span>
<span class="hljs-comment">// $.ajax(&#123;</span>
<span class="hljs-comment">//     url: "mock/user.json",</span>
<span class="hljs-comment">//     success(data) &#123;</span>
<span class="hljs-comment">//         console.log("查询用户：", data);</span>
<span class="hljs-comment">//         $.ajax(&#123;</span>
<span class="hljs-comment">//             url: `mock/user_corse_$&#123;data.id&#125;.json`,</span>
<span class="hljs-comment">//             success(data) &#123;</span>
<span class="hljs-comment">//                 console.log("查询到课程：", data);</span>
<span class="hljs-comment">//                 $.ajax(&#123;</span>
<span class="hljs-comment">//                     url: `mock/corse_score_$&#123;data.id&#125;.json`,</span>
<span class="hljs-comment">//                     success(data) &#123;</span>
<span class="hljs-comment">//                         console.log("查询到分数：", data);</span>
<span class="hljs-comment">//                     &#125;,</span>
<span class="hljs-comment">//                     error(error) &#123;</span>
<span class="hljs-comment">//                         console.log("出现异常了：" + error);</span>
<span class="hljs-comment">//                     &#125;</span>
<span class="hljs-comment">//                 &#125;);</span>
<span class="hljs-comment">//             &#125;,</span>
<span class="hljs-comment">//             error(error) &#123;</span>
<span class="hljs-comment">//                 console.log("出现异常了：" + error);</span>
<span class="hljs-comment">//             &#125;</span>
<span class="hljs-comment">//         &#125;);</span>
<span class="hljs-comment">//     &#125;,</span>
<span class="hljs-comment">//     error(error) &#123;</span>
<span class="hljs-comment">//         console.log("出现异常了：" + error);</span>
<span class="hljs-comment">//     &#125;</span>
<span class="hljs-comment">// &#125;);</span>


<span class="hljs-comment">//1、Promise可以封装异步操作</span>
<span class="hljs-comment">// let p = new Promise((resolve, reject) =&gt; &#123;</span>
<span class="hljs-comment">//     //1、异步操作</span>
<span class="hljs-comment">//     $.ajax(&#123;</span>
<span class="hljs-comment">//         url: "mock/user.json",</span>
<span class="hljs-comment">//         success: function (data) &#123;</span>
<span class="hljs-comment">//             console.log("查询用户成功:", data)</span>
<span class="hljs-comment">//             resolve(data);</span>
<span class="hljs-comment">//         &#125;,</span>
<span class="hljs-comment">//         error: function (err) &#123;</span>
<span class="hljs-comment">//             reject(err);</span>
<span class="hljs-comment">//         &#125;</span>
<span class="hljs-comment">//     &#125;);</span>
<span class="hljs-comment">// &#125;);</span>

<span class="hljs-comment">// p.then((obj) =&gt; &#123;</span>
<span class="hljs-comment">//     return new Promise((resolve, reject) =&gt; &#123;</span>
<span class="hljs-comment">//         $.ajax(&#123;</span>
<span class="hljs-comment">//             url: `mock/user_corse_$&#123;obj.id&#125;.json`,</span>
<span class="hljs-comment">//             success: function (data) &#123;</span>
<span class="hljs-comment">//                 console.log("查询用户课程成功:", data)</span>
<span class="hljs-comment">//                 resolve(data);</span>
<span class="hljs-comment">//             &#125;,</span>
<span class="hljs-comment">//             error: function (err) &#123;</span>
<span class="hljs-comment">//                 reject(err)</span>
<span class="hljs-comment">//             &#125;</span>
<span class="hljs-comment">//         &#125;);</span>
<span class="hljs-comment">//     &#125;)</span>
<span class="hljs-comment">// &#125;).then((data) =&gt; &#123;</span>
<span class="hljs-comment">//     console.log("上一步的结果", data)</span>
<span class="hljs-comment">//     $.ajax(&#123;</span>
<span class="hljs-comment">//         url: `mock/corse_score_$&#123;data.id&#125;.json`,</span>
<span class="hljs-comment">//         success: function (data) &#123;</span>
<span class="hljs-comment">//             console.log("查询课程得分成功:", data)</span>
<span class="hljs-comment">//         &#125;,</span>
<span class="hljs-comment">//         error: function (err) &#123;</span>
<span class="hljs-comment">//         &#125;</span>
<span class="hljs-comment">//     &#125;);</span>
<span class="hljs-comment">// &#125;)</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">get</span>(<span class="hljs-params">url, data</span>) </span>&#123;
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =&gt;</span> &#123;
        $.ajax(&#123;
            url: url,
            data: data,
            success: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>&#123;
                resolve(data);
            &#125;,
            error: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>&#123;
                reject(err)
            &#125;
        &#125;)
    &#125;);
&#125;

<span class="hljs-keyword">get</span>("mock/user.json")
    .then((data) =&gt; &#123;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"用户查询成功~~~:"</span>, data)
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">get</span>(`mock/user_corse_$&#123;data.id&#125;.json<span class="hljs-string">`);</span>
<span class="hljs-string">&#125;)</span>
<span class="hljs-string">    .then((data) =&gt; &#123;</span>
<span class="hljs-string">    console.log("课程查询成功~~~:", data)</span>
<span class="hljs-string">    return get(`</span>mock/corse_score_$&#123;data.id&#125;.json<span class="hljs-string">`);</span>
<span class="hljs-string">&#125;)</span>
<span class="hljs-string">    .then((data)=&gt;&#123;</span>
<span class="hljs-string">    console.log("课程成绩查询成功~~~:", data)</span>
<span class="hljs-string">&#125;)</span>
<span class="hljs-string">    .catch((err)=&gt;&#123;</span>
<span class="hljs-string">    console.log("出现异常",err)</span>
<span class="hljs-string">&#125;);</span></code></pre>

<h2 id="模块化"><a href="#模块化" class="headerlink" title="模块化"></a>模块化</h2><blockquote>
<p>如果我们想在写的代码里面调用其他js，就可以采用模块化的方法</p>
</blockquote>
<pre><code class="hljs js"><span class="hljs-comment">//export  导出</span>
varname =<span class="hljs-string">"jack"</span>
<span class="hljs-keyword">var</span> age=<span class="hljs-number">21</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">add</span>(<span class="hljs-params">a,b</span>)</span>&#123;
    <span class="hljs-keyword">return</span> a+b;
&#125;
<span class="hljs-keyword">export</span> &#123;name,age,add&#125;

<span class="hljs-comment">//import 导入</span>
<span class="hljs-keyword">import</span> xx <span class="hljs-keyword">from</span> <span class="hljs-string">"./hello.js"</span>
<span class="hljs-keyword">import</span> &#123;name,age,add&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">"./user.js"</span>

<span class="hljs-comment">//使用</span>
<span class="hljs-keyword">let</span> res = add(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)
<span class="hljs-built_in">console</span>.log(res)</code></pre>


            </article>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E9%A1%B9%E7%9B%AE%E7%AC%94%E8%AE%B0/">项目笔记</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0/">箭头函数</a>
                    
                      <a class="hover-with-bg" href="/tags/ES6/">ES6</a>
                    
                      <a class="hover-with-bg" href="/tags/promist/">promist</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext row">
                  <div class="post-prev col-6">
                    
                    
                      <a href="/2020/07/23/jianzhi-offer/46.%20%E6%8A%8A%E6%95%B0%E5%AD%97%E7%BF%BB%E8%AF%91%E6%88%90%E5%AD%97%E7%AC%A6%E4%B8%B2/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【剑指Offer】把数字翻译成字符串</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </div>
                  <div class="post-next col-6">
                    
                    
                      <a href="/2020/07/22/jianzhi-offer/45.%20%E6%8A%8A%E6%95%B0%E7%BB%84%E6%8E%92%E6%88%90%E6%9C%80%E5%B0%8F%E7%9A%84%E6%95%B0/">
                        <span class="hidden-mobile">【剑指Offer】把数组排成最小的数</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </div>
                </div>
              
            </div>

            
              <!-- Comments -->
              <div class="comments" id="comments">
                
                
  <div id="vcomments"></div>
  <script type="text/javascript">
    function loadValine() {
      addScript('https://cdn.staticfile.org/valine/1.4.14/Valine.min.js', function () {
        new Valine({
          el: "#vcomments",
          app_id: "NFojgagn3zsFUjT0FGVVHUTM-gzGzoHsz",
          app_key: "9R1L32c7WEvSGSP48NAnfStY",
          placeholder: "说点什么",
          path: window.location.pathname,
          avatar: "retro",
          meta: ["nick","mail","link"],
          pageSize: "10",
          lang: "zh-CN",
          highlight: false,
          recordIP: true,
          serverURLs: "https://nfojgagn.lc-cn-n1-shared.com",
        });
      });
    }
    createObserver(loadValine, 'vcomments');
  </script>
  <noscript>Please enable JavaScript to view the <a href="https://valine.js.org" target="_blank" rel="nofollow noopener noopener">comments
      powered by Valine.</a></noscript>


              </div>
            
          </div>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div id="tocbot"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    
  </main>

  
    <a id="scroll-top-button" href="#" role="button">
      <i class="iconfont icon-arrowup" aria-hidden="true"></i>
    </a>
  

  
    <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
  

  

  

  <footer class="mt-5">
  <div class="text-center py-3">
  <p id="hitokoto">:D 获取中...</p>
<script>
  fetch('https://v1.hitokoto.cn')
    .then(response => response.json())
    .then(data => {
      const hitokoto = document.getElementById('hitokoto')
      hitokoto.innerText = data.hitokoto
      })
      .catch(console.error)
</script>
   <div>
  <span id="timeDate">载入天数...</span>
  <span id="times">载入时分秒...</span>
  <script>
  var now = new Date();
  function createtime(){
      var grt= new Date("07/18/2020 18:00:00");//此处修改你的建站时间或者网站上线时间
      now.setTime(now.getTime()+250);
      days = (now - grt ) / 1000 / 60 / 60 / 24;
      dnum = Math.floor(days);
      hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum);
      hnum = Math.floor(hours);
      if(String(hnum).length ==1 ){
          hnum = "0" + hnum;
      }
      minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
      mnum = Math.floor(minutes);
      if(String(mnum).length ==1 ){
                mnum = "0" + mnum;
      }
      seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
      snum = Math.round(seconds);
      if(String(snum).length ==1 ){
                snum = "0" + snum;
      }
      document.getElementById("timeDate").innerHTML = "本站安全运行&nbsp"+dnum+"&nbsp天";
      document.getElementById("times").innerHTML = hnum + "&nbsp小时&nbsp" + mnum + "&nbsp分&nbsp" + snum + "&nbsp秒";
  }
  setInterval("createtime()",250);
  </script>
</div>
    <div>
      &copy;2019 - 2020 By Jimu98
    </div>
    

    
  <!-- 备案信息 -->
  <div class="beian">
		
          <img src="/img/police_beian.png" srcset="/img/loading.gif" alt="police-icon" />
        
    <a href="http://beian.miit.gov.cn/" target="_blank"
       rel="nofollow noopener">冀ICP备19035627号</a>
    
  </div>


    
      <!-- cnzz Analytics Icon -->
      <span id="cnzz_stat_icon_1278280985" style="display: none"></span>
    
  </div>

</footer>

<!-- SCRIPTS -->
<script  src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js" ></script>
<script  src="https://cdn.staticfile.org/twitter-bootstrap/4.4.1/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/main.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  <script defer src="https://cdn.staticfile.org/clipboard.js/2.0.6/clipboard.min.js" ></script>
  <script  src="/js/clipboard-use.js" ></script>







  <script  src="https://cdn.staticfile.org/tocbot/4.11.1/tocbot.min.js" ></script>
  <script>
    $(document).ready(function () {
      var boardCtn = $('#board-ctn');
      var boardTop = boardCtn.offset().top;

      tocbot.init({
        tocSelector: '#tocbot',
        contentSelector: 'article.markdown-body',
        headingSelector: 'h1,h2,h3,h4,h5,h6',
        linkClass: 'tocbot-link',
        activeLinkClass: 'tocbot-active-link',
        listClass: 'tocbot-list',
        isCollapsedClass: 'tocbot-is-collapsed',
        collapsibleClass: 'tocbot-is-collapsible',
        collapseDepth: 0,
        scrollSmooth: true,
        headingsOffset: -boardTop
      });
      if ($('.toc-list-item').length > 0) {
        $('#toc').css('visibility', 'visible');
      }
    });
  </script>



  <script  src="https://cdn.staticfile.org/typed.js/2.0.11/typed.min.js" ></script>
  <script>
    var typed = new Typed('#subtitle', {
      strings: [
        '  ',
        "【项目笔记】ES6复习&nbsp;",
      ],
      cursorChar: "|",
      typeSpeed: 95,
      loop: false,
    });
    typed.stop();
    $(document).ready(function () {
      $(".typed-cursor").addClass("h2");
      typed.start();
    });
  </script>



  <script  src="https://cdn.staticfile.org/anchor-js/4.2.2/anchor.min.js" ></script>
  <script>
    anchors.options = {
      placement: "right",
      visible: "hover",
      
    };
    var el = "h1,h2,h3,h4,h5,h6".split(",");
    var res = [];
    for (item of el) {
      res.push(".markdown-body > " + item)
    }
    anchors.add(res.join(", "))
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    var path = "/local-search.xml";
    var inputArea = document.querySelector("#local-search-input");
    inputArea.onclick = function () {
      searchFunc(path, 'local-search-input', 'local-search-result');
      this.onclick = null
    }
  </script>



  <script  src="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.staticfile.org/fancybox/3.5.7/jquery.fancybox.min.css" />

  <script>
    $('#post img:not(.no-zoom img, img[no-zoom]), img[zoom]').each(
      function () {
        var element = document.createElement('a');
        $(element).attr('data-fancybox', 'images');
        $(element).attr('href', $(this).attr('src'));
        $(this).wrap(element);
      }
    );
  </script>







  
  
    <script>
      !function (e, t, a) {
        function r() {
          for (var e = 0; e < s.length; e++) s[e].alpha <= 0 ? (t.body.removeChild(s[e].el), s.splice(e, 1)) : (s[e].y--, s[e].scale += .004, s[e].alpha -= .013, s[e].el.style.cssText = "left:" + s[e].x + "px;top:" + s[e].y + "px;opacity:" + s[e].alpha + ";transform:scale(" + s[e].scale + "," + s[e].scale + ") rotate(45deg);background:" + s[e].color + ";z-index:99999");
          requestAnimationFrame(r)
        }

        function n() {
          var t = "function" == typeof e.onclick && e.onclick;
          e.onclick = function (e) {
            t && t(), o(e)
          }
        }

        function o(e) {
          var a = t.createElement("div");
          a.className = "heart", s.push({
            el: a,
            x: e.clientX - 5,
            y: e.clientY - 5,
            scale: 1,
            alpha: 1,
            color: c()
          }), t.body.appendChild(a)
        }

        function i(e) {
          var a = t.createElement("style");
          a.type = "text/css";
          try {
            a.appendChild(t.createTextNode(e))
          } catch (t) {
            a.styleSheet.cssText = e
          }
          t.getElementsByTagName("head")[0].appendChild(a)
        }

        function c() {
          return "rgb(" + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + "," + ~~(255 * Math.random()) + ")"
        }

        var s = [];
        e.requestAnimationFrame = e.requestAnimationFrame || e.webkitRequestAnimationFrame || e.mozRequestAnimationFrame || e.oRequestAnimationFrame || e.msRequestAnimationFrame || function (e) {
          setTimeout(e, 1e3 / 60)
        }, i(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"), n(), r()
      }(window, document);
    </script>
  











  

  

  

  

  

  
    <!-- cnzz Analytics -->
    <script defer src="//s4.cnzz.com/z_stat.php?id=1278280985&show=pic"
            type="text/javascript"></script>
  





</body>
</html>
